<template>
  <el-dialog :close-on-click-modal='false' v-drag :title="$t('添加')" :v-if="dialogVisible" :visible.sync="dialogVisible" width="400px" top="20vh" @close='close'>
    <div class="dialogContentBoxMaxHeight90">
      <el-form ref="form" :model="record" label-width="70px" :rules="rules" label-position="left" style="margin:0 30px;">
        <el-form-item label="IP" prop="ip">
          <el-input v-emoji v-model="record.ip" size='small' ref="recordIp"></el-input>
        </el-form-item>
        <el-form-item :label="$t('IP类型')" prop="iptype">
          <el-radio style="margin-left:20px;" v-model="record.iptype" :label="0" class="IPv4">IPv4</el-radio>
          <el-radio v-model="record.iptype" :label="1" class="IPv6">IPv6</el-radio>
        </el-form-item>
        <el-form-item label="MAC" prop="mac">
          <el-input v-emoji v-model="record.mac" size='small' ref="recordMac"></el-input>
        </el-form-item>
      </el-form>
    </div>
    <span slot="footer" class="dialog-footer">
      <el-button type="primary" @click="submit" class="addSubmit">{{$t('确 定')}}</el-button>
      <el-button @click="close" class="addClose">{{$t('取 消')}}</el-button>
    </span>
  </el-dialog>
</template>
<script>
import { addBind } from '@/api/safe/bind'
import { inputClass } from '@/utils/input-class.js'
export default {
  name: 'BindDialog',
  data () {
    var validateIp = (rule, value, callback) => {
      if (!String(value).trim()) {
        callback(new Error(this.$t('请输入IP')))
      } else {
        callback()
      }
    }
    var validateMac = (rule, value, callback) => {
      if (!String(value).trim()) {
        callback(new Error(this.$t('请输入MAC')))
      } else if (!this.isValidMAC(value)) {
        callback(new Error(this.$t('请输入正确的MAC地址')))
      } else {
        callback()
      }
    }
    return {
      dialogVisible: false,
      record: {
        id: null,
        ip: '',
        iptype: 0,
        mac: ''
      },
      recordCopy: {
        id: null,
        ip: '',
        iptype: 0,
        mac: ''
      },
      rules: {
        ip: [{ required: false, validator: validateIp, trigger: 'blur' }],
        mac: [{ required: false, validator: validateMac, trigger: 'blur' }]
      }
    }
  },
  methods: {
    init (data) {
      this.dialogVisible = true
      this.$nextTick(() => {
        const data = ['recordIp', 'recordMac']
        inputClass(this.$refs, data)
      })
    },
    submit () {
      this.$refs.form.validate((valid) => {
        if (valid) {
          addBind(this.record).then(res => {
            if (res.status === 0) {
              this.$message({
                message: this.$t('添加成功'),
                type: 'success'
              })
              this.close()
              this.$parent.listData()
            }
          })
        }
      })
    },
    close () {
      this.dialogVisible = false
      this.record = Object.assign({}, this.recordCopy)
      this.$refs.form.resetFields()
    },
    // 验证MAC地址
    isValidMAC (mac) {
      let reg = /(([A-Fa-f0-9]{2}-){5}[A-Fa-f0-9]{2})|(([A-Fa-f0-9]{2}:){5}[A-Fa-f0-9]{2})/
      return reg.test(mac)
    }
  }
}
</script>
